<?php 
/**
 * Ajax_Config.phtml - Gestion de la configuration
 *
 * @package
 * @author Alexis Eric
 * @copyright Copyright (c) 2012 - Erra Prod
 * @version $1.0$
 * @access public
 */

$column_name = utf8_encode(base64_decode($_POST['column_name']));
$value = utf8_encode(base64_decode($_POST['value']));
$data_genre = utf8_encode(base64_decode($_POST['data_genre']));
$elementClass = ($data_genre == 'calendar_element') ? 'Config_ConfigObject' : 'Config_GeneralConfigObject';

Erra_Core_DatabasePdo::SELECT('SELECT ' . $column_name . ' FROM ' . strtolower($elementClass));

/**
 * Configuration AJAX
 */
if (Erra_Core_DatabasePdo::rowCount() && Access_ControlObject::is_logged()) {
    if ($data_genre == 'calendar_element') {
        $user_id = Access_ControlObject::get_user_id();
        Erra_Core_DatabasePdo::EXECUTE('UPDATE ' . strtolower($elementClass) . ' SET ' . $column_name . '="' . $value . '" WHERE user_id=' . $user_id);
        // Update Config object
        $elementClass::loadConfiguration()->$column_name->setValue($value);
    } else if ($data_genre == 'general_element') {
        Erra_Core_DatabasePdo::EXECUTE('UPDATE ' . strtolower($elementClass) . ' SET ' . $column_name . '="' . $value . '"');
        // Update Config object
        $elementClass::loadConfiguration()->$column_name->setValue($value);
    }
    echo 'success';
} else {
    echo 'failure';
}